function [zmat,ztran] = tauchen(muz,rhoz,sez,nz,m)

sz       = sez/sqrt(1-rhoz^2);
zmin     = muz-m*sz;
zmax     = muz+m*sz;
zmat     = linspace(zmin,zmax,nz)';

ztran     = zeros(nz,nz);
w         = zmat(2)-zmat(1);
term1     = zmat(1)  +w/2-(1-rhoz)*muz-rhoz*zmat;
term2     = zmat(end)-w/2-(1-rhoz)*muz-rhoz*zmat;
term3     = repmat((zmat(2:end-1))',nz,1)+w/2-(1-rhoz)*muz-rhoz*repmat(zmat,1,nz-2);
term4     = repmat((zmat(2:end-1))',nz,1)-w/2-(1-rhoz)*muz-rhoz*repmat(zmat,1,nz-2);

ztran(:,1)      =   normcdf(term1/sez);
ztran(:,nz)     = 1-normcdf(term2/sez);
ztran(:,2:nz-1) = normcdf(term3/sez)-normcdf(term4/sez);
